﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class TrainLateDAL
    {
        public static void ADD(List<TrainLateModel> lates)
        {
            foreach (var item in lates)
            {
                string sql = @"INSERT INTO `trainlate`
                            (`TrainNum`,
                            `PlatformId`,
                            `LateTime`,
                            `ExamResultsTestQuestionID`,
                            `TrainNumber`)
                            VALUES
                            (@TrainNum,
                            @PlatformId,
                            @LateTime,
                            @ExamResultsTestQuestionID,
                            @TrainNumber)";

                MySqlParameter[] parms = {
                new MySqlParameter("@TrainNum", item.TrainNum ),
                new MySqlParameter("@PlatformId", item.PlatformId),
                new MySqlParameter("@LateTime", item.LateTime ),
                new MySqlParameter("@ExamResultsTestQuestionID", item.ExamResultsTestQuestionID),
                new MySqlParameter("@TrainNumber", item.TrainNumber)
            };
                DbHelper.ExecuteNonQuery(sql, parms);
            }
        }

        public static List<TrainLateModel> GetTrainLateList(int ExamResultsTestQuestionID)
        {
            string sql = "SELECT a.*,b.devname FROM trainlate a inner join device b on a.PlatformId=b.deviceid and b.devtype=6 where ExamResultsTestQuestionID = " + ExamResultsTestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<TrainLateModel> list = new List<TrainLateModel>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetTrainLateModel(dt.Rows[i]));
                }
            }
            return list;
        }

        internal static TrainLateModel GetTrainLateModel(DataRow dr)
        {
            TrainLateModel info = new TrainLateModel();

            info.TrainNum = Convert.ToInt32(dr["TrainNum"]);
            info.PlatformId = Convert.ToInt32(dr["PlatformId"]);
            info.Platform = dr["devname"].ToString();
            info.LateTime = int.Parse(dr["LateTime"].ToString());
            info.ExamResultsTestQuestionID = int.Parse(dr["ExamResultsTestQuestionID"].ToString());
            info.TrainNumber = int.Parse(dr["TrainNumber"].ToString());
            return info;
        }
    }
}
